﻿using System;

namespace Fx.Data.NHibernate
{
    /// <summary>
    /// Provides the ability to decorate repositories with an attribute defining the factory key
    /// for the given repository; accordingly, the respective session factory will be used to 
    /// communicate with the database.  This allows you to declare different repositories to 
    /// communicate with different databases.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class)]
    public class SessionAttribute : Attribute
    {
        public readonly string FactoryKey;

        public SessionAttribute(string factoryKey)
        {
            FactoryKey = factoryKey;
        }

        /// <summary>
        /// Global method to retrieve the factory key from an object, as defined in its 
        /// SessionFactoryAttribute, if available.  Defaults to the DefaultFactoryKey 
        /// if not found.
        /// </summary>
        public static string GetKeyFrom(object anObject)
        {
            if (!SessionConfigurer.UseMultipleDatabases)
                return SessionConfigurer.DefaultFactoryKey;

            Type objectType = anObject.GetType();

            object[] attributes = objectType.GetCustomAttributes(typeof(SessionAttribute), true);

            if (attributes.Length > 0)
            {
                SessionAttribute attribute = (SessionAttribute)attributes[0];
                return attribute.FactoryKey;
            }

            return SessionConfigurer.DefaultFactoryKey;
        }
    }
}
